import { _decorator } from 'cc';
export class CircleTheCat_GameData {
    //    // 游戏配置
    public static readonly GRID_SIZE: number = 11; // 网格大小 11x11
    public static readonly CELL_SIZE: number = 60; // 每个格子的大小
    public static readonly CAT_SPEED: number = 1.0; // 猫的移动速度
    public static readonly AI_DELAY: number = 0.5; // AI思考延迟

    //    // 游戏状态
    public static gameState: 'playing' | 'won' | 'lost' = 'playing';
    public static currentLevel: number = 1;
    public static maxLevel: number = 10;
    public static score: number = 0;
    public static moves: number = 0;

    //    // 网格数据 (0=空, 1=墙, 2=猫)
    public static grid: number[][] = [];

    //    // 猫的位置
    public static catPosition: { x: number, y: number } = { x: 5, y: 5 };

    //    // 游戏设置
    public static aiEnabled: boolean = true;
    public static showHints: boolean = false;
    public static soundEnabled: boolean = true;

    //    // 初始化网格
    public static initGrid(): void {
        this.grid = [];
        for (let i = 0; i < this.GRID_SIZE; i++) {
            this.grid[i] = [];
            for (let j = 0; j < this.GRID_SIZE; j++) {
                this.grid[i][j] = 0; // 初始化为空
            }
        }

        // 设置边界墙
        for (let i = 0; i < this.GRID_SIZE; i++) {
            this.grid[0][i] = 1; // 上边界
            this.grid[this.GRID_SIZE - 1][i] = 1; // 下边界
            this.grid[i][0] = 1; // 左边界
            this.grid[i][this.GRID_SIZE - 1] = 1; // 右边界
        }

        // 设置猫的初始位置
        this.catPosition = { x: 5, y: 5 };
        this.grid[this.catPosition.y][this.catPosition.x] = 2;
    }

    //    // 重置游戏
    public static resetGame(): void {
        this.gameState = 'playing';
        this.moves = 0;
        this.initGrid();
    }

    //    // 检查游戏是否结束
    public static checkGameEnd(): boolean {
        // 检查猫是否到达边界
        if (this.catPosition.x === 0 || this.catPosition.x === this.GRID_SIZE - 1 ||
            this.catPosition.y === 0 || this.catPosition.y === this.GRID_SIZE - 1) {
            this.gameState = 'lost';
            return true;
        }

        // 检查猫是否被围住
        const directions = [
            { x: 0, y: -1 }, // 上
            { x: 1, y: 0 },  // 右
            { x: 0, y: 1 },  // 下
            { x: -1, y: 0 }  // 左
        ];

        let canMove = false;
        for (const dir of directions) {
            const newX = this.catPosition.x + dir.x;
            const newY = this.catPosition.y + dir.y;
            if (this.isValidPosition(newX, newY) && this.grid[newY][newX] === 0) {
                canMove = true;
                break;
            }
        }

        if (!canMove) {
            this.gameState = 'won';
            return true;
        }

        return false;
    }

    //    // 检查位置是否有效
    public static isValidPosition(x: number, y: number): boolean {
        return x >= 0 && x < this.GRID_SIZE && y >= 0 && y < this.GRID_SIZE;
    }

    //    // 设置墙
    public static setWall(x: number, y: number): boolean {
        if (this.isValidPosition(x, y) && this.grid[y][x] === 0) {
            this.grid[y][x] = 1;
            this.moves++;
            return true;
        }
        return false;
    }

    //    // 移动猫
    public static moveCat(): void {
        if (this.gameState !== 'playing') return;

        const directions = [
            { x: 0, y: -1 }, // 上
            { x: 1, y: 0 },  // 右
            { x: 0, y: 1 },  // 下
            { x: -1, y: 0 }  // 左
        ];

        // 找到最佳移动方向
        let bestDirection = null;
        let bestScore = -Infinity;

        for (const dir of directions) {
            const newX = this.catPosition.x + dir.x;
            const newY = this.catPosition.y + dir.y;

            if (this.isValidPosition(newX, newY) && this.grid[newY][newX] === 0) {
                const score = this.evaluatePosition(newX, newY);
                if (score > bestScore) {
                    bestScore = score;
                    bestDirection = dir;
                }
            }
        }

        // 执行移动
        if (bestDirection) {
            this.grid[this.catPosition.y][this.catPosition.x] = 0;
            this.catPosition.x += bestDirection.x;
            this.catPosition.y += bestDirection.y;
            this.grid[this.catPosition.y][this.catPosition.x] = 2;
        }
    }

    //    // 评估位置分数
    private static evaluatePosition(x: number, y: number): number {
        // 距离边界的距离（越近越好）
        const distanceToEdge = Math.min(x, y, this.GRID_SIZE - 1 - x, this.GRID_SIZE - 1 - y);

        // 可移动方向数量
        const directions = [
            { x: 0, y: -1 }, { x: 1, y: 0 }, { x: 0, y: 1 }, { x: -1, y: 0 }
        ];
        let moveOptions = 0;
        for (const dir of directions) {
            const newX = x + dir.x;
            const newY = y + dir.y;
            if (this.isValidPosition(newX, newY) && this.grid[newY][newX] === 0) {
                moveOptions++;
            }
        }

        return distanceToEdge * 10 + moveOptions * 5;
    }
}